package com.aube.interact.util;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.aube.interact.R;

public class AnimatorHelper {

    public static final int DURATION = 300;

    public static void voteAnimator(final View view, int resId) {
        float scale = 1.5f;
        int paddingLeft = view.getPaddingLeft();
        int paddingTop = view.getPaddingTop();
        int width = view.getWidth() - paddingLeft - view.getPaddingRight();
        int height = view.getHeight() - paddingTop - view.getPaddingBottom();
        int layoutWidth = (int) (width * scale);
        int layoutHeight = (int) (height * scale);
        Context context = view.getContext();
        FrameLayout layout = new FrameLayout(context);
        ImageView imageView = new ImageView(context);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        layout.addView(imageView, params);
        imageView.setImageResource(resId);
        final PopupWindow window = new PopupWindow(layout, layoutWidth, layoutHeight);
        //window.showAsDropDown(view, (int)(width * (scale - 1) / 2), (int)(-height * (scale + 1) / 2));
        int[] pos = new int[2];
        view.getLocationOnScreen(pos);
        int posX = pos[0] + paddingLeft + width / 2 - layoutWidth / 2;
        int poxY = pos[1] + paddingTop + height / 2 - layoutHeight / 2;
        window.showAtLocation(view, Gravity.NO_GRAVITY, posX, poxY);

        ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, scale);
        scaleXAnim.setDuration(DURATION / 2);

        ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, scale);
        scaleYAnim.setDuration(DURATION / 2);

        ObjectAnimator scaleOutXAnim = ObjectAnimator.ofFloat(imageView, "scaleX", scale, 1f);
        scaleOutXAnim.setDuration(DURATION / 2);
        scaleOutXAnim.setStartDelay(DURATION / 2);

        ObjectAnimator scaleOutYAnim = ObjectAnimator.ofFloat(imageView, "scaleY", scale, 1f);
        scaleOutYAnim.setDuration(DURATION / 2);
        scaleOutYAnim.setStartDelay(DURATION / 2);

        AnimatorSet anim = new AnimatorSet();
        anim.setDuration(DURATION);
        anim.playTogether(scaleXAnim, scaleYAnim, scaleOutXAnim, scaleOutYAnim);
        anim.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                view.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.VISIBLE);
                //((ViewGroup)imageView.getParent()).removeView(imageView);
                window.dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                view.setVisibility(View.VISIBLE);
                //((ViewGroup)imageView.getParent()).removeView(imageView);
                window.dismiss();
            }
        });
        anim.start();
    }

    public static void voteAnimation(final View view) {
        Animation anim = AnimationUtils.loadAnimation(view.getContext(), R.anim.breath_anim);
        anim.setDuration(150);
        view.startAnimation(anim);
    }

    public static void voteAnimation(final ImageButton view, final boolean isSelected, final boolean isRed) {
        Animation anim = AnimationUtils.loadAnimation(view.getContext(), R.anim.breath_anim);
        anim.setDuration(150);
        if (!isSelected) {
            anim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    view.setImageResource(isRed ? R.drawable.icon_vswhitel : R.drawable.icon_vswhiter);
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    view.setImageResource(isRed ? R.drawable.icon_vsred : R.drawable.icon_vsblue);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
        view.startAnimation(anim);
    }

    public static void voteAnimation(final ImageView view, final Animation.AnimationListener listener) {
        Animation anim = AnimationUtils.loadAnimation(view.getContext(), R.anim.breath_anim);
        anim.setDuration(150);
        if (listener != null)
            anim.setAnimationListener(listener);
        view.startAnimation(anim);
    }

    public static void alphaAnim(final View view, final float from, final float to) {
        alphaAnim(view, from, to, true);
    }

    public static void alphaAnim(final View view, final float from, final float to, final boolean useGone) {
        AlphaAnimation anim = new AlphaAnimation(from, to);
        anim.setDuration(150);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                if (to > from)
                    view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (from > to)
                    view.setVisibility(useGone ? View.GONE : View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(anim);
    }

}
